#include "mymsgbox.h"


static void msgbox_1(void)
{
    static const char *btns[] = { "Continue", "Close", "" };
    lv_obj_t *msgbox = lv_msgbox_create( lv_scr_act(), "Notice", "Do you want to continue?", btns, false );
}


lv_obj_t *ta;
lv_obj_t *ta_password;
lv_obj_t *label_user;
lv_obj_t *label_password;
lv_obj_t *keyboard;
lv_obj_t *msgbox;

static void event_cb(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* target = lv_event_get_target(e);
    if(target == ta)
    {
        if(code == LV_EVENT_FOCUSED) //选中与键盘进行关联
        {
            lv_keyboard_set_textarea(keyboard,ta);
        }
        else if(code == LV_EVENT_VALUE_CHANGED)
        {
            const char* txt = lv_textarea_get_text(ta);
            if(strcmp(txt, "admin") == 0)
                lv_label_set_text(label_user, LV_SYMBOL_OK);//标签设置正确图标
            else
                lv_label_set_text(label_user, "");
        }
    }
    else if(target == ta_password)
    {
        if(code == LV_EVENT_FOCUSED)
        {
            lv_keyboard_set_textarea(keyboard,ta_password);
        }
        else if(code == LV_EVENT_VALUE_CHANGED)
        {
            const char* txt2 = lv_textarea_get_text(ta_password);
            if(strcmp(txt2, "123456") == 0)
                lv_label_set_text(label_password, LV_SYMBOL_OK);
            else
                lv_label_set_text(label_password, "");
        }
    }
}

static void event_cb_msgbox(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_current_target(e);
    if(code == LV_EVENT_VALUE_CHANGED)
    {
        const char* txt = lv_textarea_get_text(ta);
        const char* txt3 = lv_textarea_get_text(ta_password);
        if(strcmp(txt, "admin") == 0 && strcmp(txt3, "123456") == 0)
                lv_obj_clear_flag(msgbox, LV_OBJ_FLAG_HIDDEN);//清除隐藏标志
    }
    else if(code == LV_EVENT_PRESSED)
    {
        if(lv_msgbox_get_active_btn(msgbox) == 2)    /* 获取按钮索引 */
            lv_obj_add_flag(msgbox, LV_OBJ_FLAG_HIDDEN);  /* 添加隐藏标志 */
    }
}
static void msgbox_2(void)
{
//1.设置用户输入文本框
    ta = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(ta, 300, 40);
    lv_obj_align(ta, LV_ALIGN_TOP_MID, 0, 40);
    lv_obj_remove_style(ta, NULL, LV_PART_SCROLLBAR);    //移除滚动条样式
    // lv_textarea_set_password_mode(ta, true);
    lv_textarea_set_placeholder_text(ta, "username");
    lv_obj_add_event_cb(ta, event_cb, LV_EVENT_ALL,NULL);
//2.设置密码输入文本框
    ta_password = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(ta_password, 300, 40);
    lv_obj_align_to(ta_password,ta,LV_ALIGN_OUT_BOTTOM_MID, 0, 40);
    lv_obj_remove_style(ta_password, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_password_mode(ta_password, true);
    lv_textarea_set_placeholder_text(ta_password, "password");
    lv_obj_add_event_cb(ta_password, event_cb, LV_EVENT_ALL,NULL);
//3.创建user标签
    label_user = lv_label_create(lv_scr_act());
    lv_label_set_text(label_user, "");
    lv_obj_set_style_text_font(label_user, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_align_to(label_user,ta, LV_ALIGN_OUT_RIGHT_MID,30,0);
//4.创建password标签
    label_password = lv_label_create(lv_scr_act());
    lv_label_set_text(label_password, "");
    lv_obj_set_style_text_font(label_password, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_align_to(label_password,ta_password, LV_ALIGN_OUT_RIGHT_MID,30,0);
//5.设置键盘
    keyboard = lv_keyboard_create(lv_scr_act());

//6.设置消息框
     static const char *btns[] = { " ", " ", "OK","" };
     msgbox = lv_msgbox_create( lv_scr_act(), LV_SYMBOL_SETTINGS"Notice", "Do you want to continue?", btns, true );
     lv_obj_set_align(msgbox, LV_ALIGN_CENTER);
     lv_obj_add_flag(msgbox, LV_OBJ_FLAG_HIDDEN);   //添加隐藏标志
     //获取到按钮部分
     lv_obj_t* btn = lv_msgbox_get_btns(msgbox);
     //按钮背景设为透明
     lv_obj_set_style_bg_opa(btn, LV_OPA_0, LV_PART_ITEMS);    //背景透明
     lv_obj_set_style_shadow_width(btn, 0, LV_PART_ITEMS);    //去除阴影
     lv_obj_set_style_text_color(btn, lv_color_hex(0xff0000),LV_PART_ITEMS);    //字体颜色
     lv_obj_add_event_cb(ta, event_cb_msgbox, LV_EVENT_VALUE_CHANGED,NULL);
     lv_obj_add_event_cb(ta_password, event_cb_msgbox, LV_EVENT_VALUE_CHANGED,NULL);
     lv_obj_add_event_cb(msgbox, event_cb_msgbox, LV_EVENT_PRESSED,NULL);
}


void mymsgbox_example(void)
{
    msgbox_2();
}

